package com.zzyl.nursing.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-12
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private static final String NURSING_PROJECT_KEY = "nursing:project";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
//        nursingProject.setCreateTime(DateUtils.getNowDate());
        int i = save(nursingProject) ? 1 : 0;
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_PROJECT_KEY);
        return i;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
//        nursingProject.setUpdateTime(DateUtils.getNowDate());
        int i = updateById(nursingProject) ? 1 : 0;
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_PROJECT_KEY);
        return i;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int i = removeByIds(Arrays.asList(ids)) ? 1 : 0;
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_PROJECT_KEY);
        return i;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        int i = removeById(id) ? 1 : 0;
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_PROJECT_KEY);
        return i;
    }

    /**
     * 查询所有的护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> listAll() {
        //先查询redis缓存
        Map<Object, Object> projects = stringRedisTemplate.opsForHash().entries(NURSING_PROJECT_KEY);
        //判断缓存是否为空
        if (!projects.isEmpty()) {
            //缓存命中
            List<NursingProjectVo> nursingProjectVos = new ArrayList<>();
            projects.forEach((k, v) -> {
                //json字符串转为对象
                NursingProjectVo nursingProjectVo = JSON.parseObject(v.toString(), NursingProjectVo.class);
                nursingProjectVos.add(nursingProjectVo);
            });
            return nursingProjectVos;
        }

        //查询数据库
        List<NursingProjectVo> nursingProjects = nursingProjectMapper.listAll();
        //处理数据
        LinkedHashMap<Object, Object> nursingProjectMap = new LinkedHashMap<>();
        nursingProjects.forEach(nursingProject -> {
            //nursingProject转为json字符串
            String json = JSON.toJSONString(nursingProject);
            nursingProjectMap.put(nursingProject.getValue().toString(), json);
        });
        //缓存到redis
        stringRedisTemplate.opsForHash().putAll(NURSING_PROJECT_KEY, nursingProjectMap);
        stringRedisTemplate.expire(NURSING_PROJECT_KEY, 1, TimeUnit.DAYS);
        //返回
        return nursingProjects;
    }

    /**
     * 封装分页信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableDataInfo selectPage(Integer pageNum, Integer pageSize) {
        //分页查询数据
        IPage page = new Page(pageNum,pageSize);
        page = page(page, null);
        return getTableDataInfo(page);
    }

    /**
     * 封装分页信息
     * @param page
     * @return
     */
    private TableDataInfo getTableDataInfo(IPage page) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("请求成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
